//
//  CYYNetWorkManager+CommentList.m
//  LetsPartyBmob
//
//  Created by rimi on 15/9/10.
//  Copyright (c) 2015年 柴阳阳. All rights reserved.
//

#import "CYYNetWorkManager+CommentList.h"

@implementation CYYNetWorkManager (CommentList)

//获得某个活动的评论列表
+ (void)commentListWithPartyObjectId:(NSString *)partyObjectId
                              sucess:(SucessCommentList)sucess
                                fail:(Fail)fail{
    //关联对象表
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"Comments"];
    
    //需要查询的列
    BmobObject * party = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
    [bquery whereObjectKey:@"partyComments" relatedTo:party];
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            fail(error);
        } else {
            sucess(array);
        }
    }];
}

//获得某个活动的评论列表NEW
+ (void)newCommentListWithPartyObjectId:(NSString *)partyObjectId
                              sucess:(SucessCommentListArray)sucess
                                fail:(Fail)fail{
    NSMutableArray * commmentArray = [NSMutableArray array];
    //关联对象表
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"Comments"];
    
    //需要查询的列
    BmobObject * party = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
    
    [bquery whereObjectKey:@"partyComments" relatedTo:party];
    //降序
    [bquery orderByDescending:@"createdAt"];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray * array, NSError *error) {
        if (error) {
            fail(error);
        } else {
            for (int i = 0; i < array.count; i++) {
                NSMutableDictionary * commmentDict = [NSMutableDictionary dictionary];
                commmentDict[@"objectId"] = ((BmobObject *)array[i]).objectId;
                commmentDict[@"commentAuthorName"] = [array[i] objectForKey:@"commentAuthorName"];
                commmentDict[@"commentContent"] = [array[i] objectForKey:@"commentContent"];
                commmentDict[@"createdAt"] = [self dateToString:((BmobObject *)array[i]).createdAt];
                [commmentArray addObject:commmentDict];
            }
            sucess(commmentArray);
        }
    }];
}

+ (NSString *)dateToString:(NSDate *)date{
    NSDateFormatter * dateFormater = [[NSDateFormatter alloc]init];
    [dateFormater setDateFormat:@"yyyy/MM/dd HH:mm"];
    return [dateFormater stringFromDate:date];
}

/**
 *  点赞
 */

+ (void)supportACommmet{
    
}

/**
 *  取消点赞
 */

+ (void)deSuppotAComment{
    
}


@end
